package com.crcode.crm.dao.Industry;

import com.crcode.crm.bean.Industry.IndustryBean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lmz
 * @company Peter
 * @create 2019/3/25  11:49
 */

public interface IndustryDao extends JpaRepository<IndustryBean, Integer>, JpaSpecificationExecutor<IndustryBean> {


    /**
     * 更具uuid查询所有子集的uuid
     *
     * @param uuid
     * @return
     */
    @Query("select m.uuid from IndustryBean m where m.puuid =?1")
    public List<String> findByPuuid(String uuid);

    /**
     * 根据uuid集合删除
     *
     * @param uuids
     * @return
     */
    @Transactional
    public int deleteIndustryBeansByUuidIn(List<String> uuids);

    /**
     * 根据名称查询该名字是否存在
     */
    @Query("select count(m.indName) from IndustryBean m where m.indName=?1")
    public int findByIndname(String indName);

    /**
     * 名字和puuid查询（新增数据）
     *
     * @param indName
     * @param puuid
     * @return
     */
    @Query("select count(m.indName) from IndustryBean m where m.indName=?1 and m.puuid=?2")
    public int findByPuuidAndIndname(String indName, String puuid);

    /**
     * 名字，puuid等于并且uuid不等于（编辑修改数据）
     *
     * @param puuid
     * @param indName
     * @param uuid
     * @return
     */
    @Query("select count(m.indName) from IndustryBean m where m.indName=?2 and m.puuid=?1 and m.uuid <>?3")
    public int findByPuuidAndIndNameAndUuidNot(String puuid, String indName, String uuid);

    /**
     * 更具uuid查询是否存在数据
     */
    @Query("select count(m.indName) from IndustryBean m where m.uuid=?1")
    public int findByuuid(String uuid);


    /**
     * 根据puuid和uuid查询
     * @param puuid puuid
     * @return
     */
    List<IndustryBean> findByPuuidAndUuidNotNull(String puuid);
}
